// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init({
    env: 'zbdc-6gavbhj633c62e27'
})
const db = cloud.database()
// 云函数入口函数
exports.main = async (event, context) => {
    let wordList = []
    let res = await db.collection('cs')
        .field({
            headWord: true,
            content: true,
            _id: true
        })
        .get()

    new Promise(() => {
        res.data.forEach(item => {
            // 拼接samples
            let sms = item.content.word.content.sentence ? item.content.word.content.sentence.sentences ? item.content.word.content.sentence.sentences : [] : []
            sms = sms.map((itm,index) =>{
                return itm.sCn.concat('\n' +itm.sContent) // 拼接
            }).join('\n')
            // 拼接phrase
            let phr = item.content.word.content.phrase ? item.content.word.content.phrase.phrases ? item.content.word.  content.phrase.phrases : [] : []
            phr = phr.slice(0,3).map((itm,idx) =>{
                return itm.pCn.concat('\n' + itm.pContent)
            }).join('\n')
            // 拼接definition
            let def = item.content.word.content.trans
            def = def.map((itm,idx) =>{
                return itm.pos.concat(itm.tranCn)
            }).join('\n')
            console.log(def)
            wordList.push({ // 组成集合
                _id: item._id,
                content: item.headWord,
                type: 'cet6',
                pron: item.content.word.content.phone || item.content.word.content.usphone,
                definition: def, // 要拼
                samples: sms, // 要拼
                phrase: phr // 要拼
            })
        })
    })
    return wordList
}